<?php

/**
 * This is the model class for table "tbl_user".
 *
 * The followings are the available columns in table 'tbl_user':
 * @property integer $id
 * @property string $username
 * @property string $first_name
 * @property string $last_name
 * @property string $email
 * @property string $create_time
 * @property string $update_time
 * @property string $password
 * @property string $last_login_time
 *
 * The followings are the available model relations:
 * @property Article[] $articles
 * @property Article[] $articles1
 * @property Article[] $articles2
 * @property Service[] $services
 * @property Service[] $services1
 * @property UserProperty[] $userProperties
 */
class User extends SeoVietActiveRecord
{
        public $password_repeat;
        
        //PERFORM ONE WAY ENCRYPTION ON PASSWORD BEFORE STORING TO DB
        protected function afterValidate(){
            parent::afterValidate();
            $this->password = $this->encrypt($this->password);
        }

        public function encrypt($value){
            return md5($value);
        }


	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, first_name, last_name, email, password', 'required'),
			array('email','email'),
			array('username','unique'),
			array('username, password', 'length', 'max'=>256),
			array('first_name, last_name, email', 'length', 'max'=>128),
			array('update_time, last_login_time', 'safe'),

                        array('password', 'compare'),
                        array('password_repeat', 'safe'),

			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, first_name, last_name, email, create_time, update_time, password, last_login_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'articles' => array(self::HAS_MANY, 'Article', 'author_id'),
			'articles1' => array(self::HAS_MANY, 'Article', 'editor_id'),
			'articles2' => array(self::HAS_MANY, 'Article', 'approver_id'),
			'services' => array(self::HAS_MANY, 'Service', 'create_user_id'),
			'services1' => array(self::HAS_MANY, 'Service', 'update_user_id'),
			'userProperties' => array(self::HAS_MANY, 'UserProperty', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Username',
			'first_name' => 'First Name',
			'last_name' => 'Last Name',
			'email' => 'Email',
			'create_time' => 'Create Time',
			'update_time' => 'Update Time',
			'password' => 'Password',
			'last_login_time' => 'Last Login Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('first_name',$this->first_name,true);
		$criteria->compare('last_name',$this->last_name,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('last_login_time',$this->last_login_time,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}